package com.hua.ui;

import javax.swing.*;


import javax.swing.border.EmptyBorder;
import javax.swing.table.*;

import com.hua.Model.FileInfo;
import com.hua.Model.UploadInfo;
import com.hua.Service.ProgressListener;
import com.hua.Service.funtion;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

public class routine extends JPanel{
	// 进度条
	List<JProgressBar> progressBars = new ArrayList<>();

	private int width = 300, height = 300;
	private ImageIcon image;
	ArrayList<UploadInfo> uArray_upload = new ArrayList<>();
	private JPanel contentPane;
	private JTable table;
	
	public routine() {
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 888, 480);
		add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(240, 37, 300, 300);
		panel.add(lblNewLabel);
		image = new ImageIcon("src/main/resources/images/black_task.png");
		Image img = image.getImage();
		img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		image.setImage(img);
		lblNewLabel.setIcon(image);

		JButton btnNewButton = new JButton("暂无任务");
		btnNewButton.setBounds(314, 391, 169, 50);
		panel.add(btnNewButton);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public routine(List<UploadInfo> files) {
		setLayout(null);
		
		uArray_upload.clear();
		// 添加所有任务数据
		uArray_upload.addAll(files);
		System.out.println("fileli"+uArray_upload.get(0).getName()+uArray_upload.size());

		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 888, 480);
		add(panel);

		String[] columnNames = new String[] { "内容" };
		Object[][] data = new Object[uArray_upload.size()][1];
		for (int i = 0; i < uArray_upload.size(); i++) {
			final int index = i;
			// 记录面板
			JPanel panel_1 = new JPanel();
			panel_1.setBounds(10, 289, 530, 80);
			add(panel_1);
			panel_1.setLayout(null);

			JLabel lblNewLabel = new JLabel("");
			if (uArray_upload.get(i).getType().equals("photo")) {
				lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/photo.png"));
			} else if (uArray_upload.get(i).getType().equals("document")) {
				lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/document.png"));

			} else if (uArray_upload.get(i).getType().equals("audio")) {
				lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/audio.png"));

			} else if (uArray_upload.get(i).getType().equals("movie")) {
				lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/movie.png"));

			} else {
				lblNewLabel.setIcon(new ImageIcon("src/main/resources/images/more.png"));
			}
			lblNewLabel.setBounds(14, 13, 56, 54);
			panel_1.add(lblNewLabel);

			JLabel lblNewLabel_1 = new JLabel(uArray_upload.get(i).getName());
			lblNewLabel_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 15));
			lblNewLabel_1.setBounds(76, 13, 400, 30);
			panel_1.add(lblNewLabel_1);

			// 上传进度条
			JProgressBar progressBar = new JProgressBar();
			// 设置进度条的最小值和最大值
			progressBar.setMinimum(0);
			progressBar.setMaximum(100);

			// 设置进度条的初始值
			progressBar.setValue(100);

			// 设置进度条显示数字
			progressBar.setStringPainted(true);
			progressBar.setBounds(76, 44, 240, 18);
			//
			progressBars.add(progressBar);
			panel_1.add(progressBar);

			JLabel lblNewLabel_3 = new JLabel("大小：" + uArray_upload.get(i).getSize() + "MB");
			lblNewLabel_3.setBounds(340, 44, 240, 18);
			lblNewLabel_3.setForeground(new Color(63, 112, 255));
			panel_1.add(lblNewLabel_3);
			
			if(files.get(i).getDistinction().equals("上传")){
				//上传
				JButton btnNewButton = new JButton("上传完毕");
				btnNewButton.setBounds(760, 27, 100, 27);
				panel_1.add(btnNewButton);
				
				btnNewButton.addActionListener(e -> {
					funtion.tips("查看文件", "可在文件模块中查看或搜索");
				});
			}else if(files.get(i).getDistinction().equals("下载")){
				int index1=i;
				//下载
				JButton btnNewButton = new JButton("查看文件");
				btnNewButton.setBounds(760, 27, 100, 27);
				btnNewButton.setForeground(Color.white); // 设置按钮文字颜色为红色
				btnNewButton.setBackground(new Color(253, 94, 43)); // 设置按钮背景颜色为黄色
				btnNewButton.setBorder(null); // 设置按钮无边框
				btnNewButton.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 设置圆角边框
				panel_1.add(btnNewButton);
				
				btnNewButton.addActionListener(e -> {
					//资源管理器打开
					try {
			            // 指定要打开的目录
			            File directory = new File(files.get(index1).getPath());
			            // 检查当前平台是否支持桌面操作
			            if (Desktop.isDesktopSupported()) {
			                // 获取系统桌面实例
			                Desktop desktop = Desktop.getDesktop();

			                // 使用系统资源管理器打开指定目录
			                desktop.open(directory);
			            } else {
			                System.out.println("当前平台不支持打开文件夹");
			                funtion.tips("当前平台不支持打开文件夹", "下载路径:\n"+files.get(index1).getPath());
			            }
			        } catch (IOException e1) {
			            e1.printStackTrace();
			        }
				});
			}
			
			
			
			
			
            //添加到Jtable
            data[i][0] = panel_1;
		}

		panel.setLayout(null);

		table = new JTable(new DefaultTableModel(data, columnNames) {
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		});
		// 组件的定义
		table.setRowHeight(80);
		table.setTableHeader(null);
		table.setShowGrid(false);
		JScrollPane jsp = new JScrollPane(table);
		jsp.setLocation(0, 0);
		jsp.setSize(530, 349);
		panel.add(jsp);

		// 创建一个 JScrollPane，将 JList 作为参数传入
		JScrollPane scrollPane = new JScrollPane(jsp);
		// 将 JScrollPane 添加到 panel 中
		scrollPane.setBounds(0, 0, 888, 480);
		panel.add(scrollPane);

		// 添加自定义的单元格渲染器
		table.getColumnModel().getColumn(0).setCellRenderer(new ButtonRenderer());
		table.getColumnModel().getColumn(0).setCellEditor(new ButtonCellEditor());

	}

	// 自定义的单元格渲染器
	public class ButtonRenderer extends DefaultTableCellRenderer {
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
				int row, int column) {
			if (value instanceof JPanel) {
				return (JPanel) value;
			} else {
				return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
			}
		}
	}

	// 覆写编辑器，单击处理事件
	public class ButtonCellEditor extends DefaultCellEditor {
		private Object currentValue;

		public ButtonCellEditor() {
			super(new JTextField()); // 使用文本字段作为编辑器
		}

		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
				int column) {
			currentValue = value;
			if (value instanceof JPanel) {
				return (JPanel) value;
			} else {
				JPanel panel = new JPanel();
				panel.add(new JButton(value.toString()));
				return panel;
			}
		}

		@Override
		public Object getCellEditorValue() {
			return currentValue;
		}

		@Override
		public boolean isCellEditable(EventObject e) {
			return e instanceof MouseEvent && ((MouseEvent) e).getClickCount() == 1;
		}
	}


}
